package models

import (
	"fmt"
	"time"

	"go-papers/cache"
	orm "go-papers/database"
	"go-papers/tools"
)

type OrderStudent struct {
	OrderStudentId int    `json:"orderStudentId" gorm:"type:int(11);primary_key"` //
	StudentId      int    `json:"studentId" gorm:"type:int(11);"`                 // 关联的学生id
	OrderId        int    `json:"orderId" gorm:"type:int(11);"`                   // 订单id
	MemberId       int    `json:"memberId" gorm:"type:int(11);"`                  // 会员id
	StudentName    string `json:"studentName" gorm:"type:varchar(32);"`           // 学生姓名
	StudentAge     string `json:"studentAge" gorm:"type:varchar(18);"`            // 学生姓名
	StudentPhone   string `json:"studentPhone" gorm:"type:varchar(32);"`          // 联系电话
	StudentAddr    string `json:"studentAddr" gorm:"type:varchar(255);"`          // 住址
	StudentAddress string `json:"studentAddress" gorm:"type:varchar(255);"`       // 小区
	StudentGrade   string `json:"studentGrade" gorm:"type:varchar(32);"`          // 学生年级
	DataScope      string `json:"dataScope" gorm:"-"`
	Params         string `json:"params"  gorm:"-"`
	BaseModel
}

func (OrderStudent) TableName() string {
	return "order_student"
}

// 创建OrderStudent
func (e *OrderStudent) Create() (OrderStudent, error) {
	var doc OrderStudent
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取OrderPay
func (e *OrderStudent) GetList() []OrderStudent {
	var doc []OrderStudent
	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrderId <= 0 {
		e.OrderId = -1
	}
	table = table.Where("order_id = ?", e.OrderId)
	if err := table.Find(&doc).Error; err != nil {
		return nil
	}
	return doc
}

//
func (e *OrderStudent) GetById(id int) (OrderStudent, error) {
	var doc OrderStudent

	//cache 读取 然后返回
	cacheKey := fmt.Sprintf("orderStudent:%d", id)
	if foo, ok := cache.GoLocalCache.Get(cacheKey); ok {
		l, _ := foo.(OrderStudent)
		return l, nil
	}

	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("order_id = ?", id)

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}

	// 写入cache
	cache.GoLocalCache.Set(cacheKey, doc, 5*time.Minute)

	return doc, nil
}

// 获取OrderStudent
func (e *OrderStudent) Get() (OrderStudent, error) {
	var doc OrderStudent
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取OrderStudent带分页
func (e *OrderStudent) GetPage(pageSize int, pageIndex int) ([]OrderStudent, int, error) {
	var doc []OrderStudent

	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新OrderStudent
func (e *OrderStudent) Update(id int) (update OrderStudent, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_student_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除OrderStudent
func (e *OrderStudent) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_student_id = ?", id).Delete(&OrderStudent{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *OrderStudent) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_student_id in (?)", id).Delete(&OrderStudent{}).Error; err != nil {
		return
	}
	Result = true
	return
}
